package com.linln.admin.miss.service.impl;

import com.linln.admin.miss.domain.MineSpot;
import com.linln.admin.miss.domain.Spot;
import com.linln.admin.miss.domain.SpotPool;
import com.linln.admin.miss.repository.MineSpotRepository;
import com.linln.admin.miss.repository.SpotPoolRepository;
import com.linln.admin.miss.repository.SpotRepository;
import com.linln.admin.miss.service.SpotService;
import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author hyf
 * @date 2021/10/11
 */
@Slf4j
@Service
public class SpotServiceImpl implements SpotService {

    @Autowired
    private SpotRepository spotRepository;

    @Autowired
    private SpotPoolRepository spotPoolRepository;
    @Autowired
    private MineSpotRepository mineSpotRepository;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    public Spot getById(Long id) {
        return spotRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<Spot> getPageList(Example<Spot> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return spotRepository.findAll(example, page);
    }

    /**
     * 保存数据
     * @param spot 实体对象
     */
    @Override
    public Spot save(Spot spot) {
        return spotRepository.save(spot);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return spotRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }

    @Override
    public ResultVo getByUserId(Long userId) {
        log.info("获取字条是否已存在 userId={}",userId);
        Boolean flag = false;
        if (userId==null){
            return ResultVoUtil.error("异常信息");
        }
        Spot spot = spotRepository.findSpotByUserId(userId);
        if (spot!=null){
            flag=true;
        }
        return ResultVoUtil.success(flag);
    }

    @Override
    public Boolean putSpotByUserId(Long userId) {
        Spot spot = spotRepository.findSpotByUserId(userId);
        if (spot!=null){
            SpotPool spl = spotPoolRepository.findSpotByUserId(userId);
            if (spl==null){
                SpotPool spotPool = new SpotPool();
                spotPool.setSex(spot.getSex());
                spotPool.setUserId(spot.getUserId());
                spotPool.setSexCode(spot.getSexCode());
                spotPool.setWechatNumber(spot.getWechatNumber());
                spotPoolRepository.save(spotPool);
                MineSpot mineSpot = new MineSpot();
                mineSpot.setSpotPoolId(spotPool.getId());
                mineSpot.setSex(spot.getSex());
                mineSpot.setUserId(userId);
                mineSpot.setWechatNumber(spot.getWechatNumber());
                mineSpotRepository.save(mineSpot);
            }
            return true;
        }
        return false;
    }

    @Override
    public ResultVo saveSpot(Spot spot) {
        Spot ret = spotRepository.findSpotByUserId(spot.getUserId());
        if (ret==null){
            spotRepository.save(spot);
            return ResultVoUtil.SAVE_SUCCESS;
        }else {
            return ResultVoUtil.error("已存入");
        }
    }
}